package br.com.rafsoft.mapaentrega.server;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import br.com.rafsoft.mapaentrega.server.entity.Message;
import br.com.rafsoft.mapaentrega.server.entity.Task;
import br.com.rafsoft.mapaentrega.server.entity.User;

import com.google.android.c2dm.server.PMF;

public class DataStore {

	/**
	 * Remove this object from the data store.
	 */
	public void deleteTask(Long id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Task item = pm.getObjectById(Task.class, id);
			pm.deletePersistent(item);
		} finally {
			pm.close();
		}
	}

	public Task updateTask(Task task) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.makePersistent(task);
			return task;
		} finally {
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<Task> findAllTask() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Query query = pm.newQuery("select from " + Task.class.getName()
					+ " ");
			List<Task> list = (List<Task>) query.execute();
			if (list.size() == 0) {
				// Workaround for this issue:
				// http://code.google.com/p/datanucleus-appengine/issues/detail?id=24
				list.size();
			}

			return list;
		} catch (RuntimeException e) {
			System.out.println(e);
			throw e;
		} finally {
			pm.close();
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Task> findAllTask(String user) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Query query = pm.newQuery("select from " + Task.class.getName()
					+ " where user=='"+user+"'");
			List<Task> list = (List<Task>) query.execute();
			if (list.size() == 0) {
				// Workaround for this issue:
				// http://code.google.com/p/datanucleus-appengine/issues/detail?id=24
				list.size();
			}

			return list;
		} catch (RuntimeException e) {
			System.out.println(e);
			throw e;
		} finally {
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	public Task findTask(Long id) {
		if (id == null) {
			return null;
		}

		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Query query = pm.newQuery("select from " + Task.class.getName()
					+ " where id==" + id.toString() + " &&  user=='rafael'");
			List<Task> list = (List<Task>) query.execute();
			return list.size() == 0 ? null : list.get(0);
		} catch (RuntimeException e) {
			System.out.println(e);
			throw e;
		} finally {
			pm.close();
		}
	}

	/**
	 * Remove this object from the data store.
	 */
	public void deleteMessage(Long id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Message item = pm.getObjectById(Message.class, id);
			pm.deletePersistent(item);
		} finally {
			pm.close();
		}
	}

	public Message updateMessage(Message message) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.makePersistent(message);
			return message;
		} finally {
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<Message> findAllMessage() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Query query = pm.newQuery("select from " + Message.class.getName()
					+ " ");
			List<Message> list = (List<Message>) query.execute();
			if (list.size() == 0) {
				// Workaround for this issue:
				// http://code.google.com/p/datanucleus-appengine/issues/detail?id=24
				list.size();
			}

			return list;
		} catch (RuntimeException e) {
			System.out.println(e);
			throw e;
		} finally {
			pm.close();
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Message> findAllMessage(String user) {
			PersistenceManager pm = PMF.get().getPersistenceManager();
			try {
				Query query = pm.newQuery("select from " + Message.class.getName()
						+ " where userTo=='"+user+"'");
				List<Message> list = (List<Message>) query.execute();
				if (list.size() == 0) {
					// Workaround for this issue:
					// http://code.google.com/p/datanucleus-appengine/issues/detail?id=24
					list.size();
				}

				return list;
			} catch (RuntimeException e) {
				System.out.println(e);
				throw e;
			} finally {
				pm.close();
			}
		}

	@SuppressWarnings("unchecked")
	public Message findMessage(Long id) {
		if (id == null) {
			return null;
		}

		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Query query = pm.newQuery("select from " + Message.class.getName()
					+ " where id==" + id.toString() + " &&  userTo=='rafael'");
			List<Message> list = (List<Message>) query.execute();
			return list.size() == 0 ? null : list.get(0);
		} catch (RuntimeException e) {
			System.out.println(e);
			throw e;
		} finally {
			pm.close();
		}
	}

	/**
	 * Remove this object from the data store.
	 */
	public void deleteUser(Long id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			User item = pm.getObjectById(User.class, id);
			pm.deletePersistent(item);
		} finally {
			pm.close();
		}
	}

	public User updateUser(User user) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.makePersistent(user);
			return user;
		} finally {
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<User> findAllUser() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Query query = pm.newQuery("select from " + User.class.getName()
					+ " where userTo=='rafael'");
			List<User> list = (List<User>) query.execute();
			if (list.size() == 0) {
				// Workaround for this issue:
				// http://code.google.com/p/datanucleus-appengine/issues/detail?id=24
				list.size();
			}

			return list;
		} catch (RuntimeException e) {
			System.out.println(e);
			throw e;
		} finally {
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	public User findUser(Long id) {
		if (id == null) {
			return null;
		}

		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Query query = pm.newQuery("select from " + User.class.getName()
					+ " where id==" + id.toString() + " &&  userTo=='rafael'");
			List<User> list = (List<User>) query.execute();
			return list.size() == 0 ? null : list.get(0);
		} catch (RuntimeException e) {
			System.out.println(e);
			throw e;
		} finally {
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	public User findUser(String user, String password) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Query query = pm.newQuery("select from " + User.class.getName()
					+ " where userName=='" + user + "' &&  password=='"+password+"'");
			List<User> list = (List<User>) query.execute();
			return list.size() == 0 ? null : list.get(0);
		} catch (RuntimeException e) {
			System.out.println(e);
			throw e;
		} finally {
			pm.close();
		}
	}

	
	
}
